
#include <algorithm>

#include "cocos2d.h"
#include "cocos-ext.h"

#include "Common/Exd/EXData.h"
#include "MainUILayer.h"

#include "Common/Misc/CmnFunc.h"
#include "CmdDef.h"
#include "JumpBlock.h"
#include "Jumper.h"

#include "StageSelectScene.h"
#include "StageScene.h"
#include "MainGameLayer.h"

using namespace cocos2d;
USING_NS_CC_EXT;

int uitag = 1;

namespace
{

	void ResetHeartPanel(UILayer* uiLayer, int heartCount)
	{
		UIPanel* heartPanel = (UIPanel*)uiLayer->getWidgetByTag(52);
		heartPanel->removeAllChildrenAndCleanUp(true);

		for(int i=0; i < heartCount; i++){
			UIImageView* heartImage = UIImageView::create();
			heartImage->setTexture("heart.png");
			heartImage->setPosition( CCPoint( i*50+30.0f, 30.0f) );

			heartPanel->addChild(heartImage);
		}
	}


	void ResetPoint(UILayer* uiLayer, int point)
	{
		UITextArea* textArea = (UITextArea*)uiLayer->getWidgetByTag(57);
		char pointText[32];
		sprintf(pointText, "%d", point);

		textArea->setFontSize(50);
		textArea->setText(pointText);
	}


	void ResetForbid(UILayer* uiLayer, int forbidColor, int forbidShape, int forbidNum)
	{
		UIPanel* forbidPanel = (UIPanel*)uiLayer->getWidgetByTag(54);
		forbidPanel->removeAllChildrenAndCleanUp(true);

		if(forbidColor != eColor_Non) {

			char colorText[1024];
			sprintf(colorText, "color%d.png", forbidColor);

			UIImageView* colorImage = UIImageView::create();
			colorImage->loadTexture(colorText,UI_TEX_TYPE_PLIST);
			colorImage->setScale(0.5f);
			colorImage->setPosition( CCPoint(20.0f, 30.0f) );
			forbidPanel->addChild(colorImage);
		}

		if(forbidShape != eShap_Non){
			char shapText[1024];
			switch(forbidShape){
			case eShap_Triangle:
				{
					sprintf(shapText, "%s", "shap1.png");
				}break;
			case eShap_Quadrilateral:
				{
					sprintf(shapText, "%s", "shap2.png");
				}break;
			case eShap_Circle:
				{
					sprintf(shapText, "%s", "shap3.png");
				}break;
			default:
				{
					sprintf(shapText, "");
				}break;
			}

			UIImageView* shapeImage = UIImageView::create();
			shapeImage->loadTexture(shapText,UI_TEX_TYPE_PLIST);
			shapeImage->setScale(0.5f);
			shapeImage->setPosition( CCPoint(60.0f, 30.0f) );
			forbidPanel->addChild(shapeImage);
		}

		if(forbidNum != 0) {
			char numberText[1024];
			sprintf(numberText, "%d", forbidNum);

			UITextArea* textArea = UITextArea::create();
			textArea->setText(numberText);
			textArea->setPosition( CCPoint(140.0f, 30.0f) );
			forbidPanel->addChild(textArea);
		}
	}

}


// on "init" you need to initialize your instance
bool MainUILayer::init(int _forbidColor, int _forbidShape, int _forbidNum, int _hp)
{
    bool bRet = false;
    do 
    {
        //////////////////////////////////////////////////////////////////////////
        // super init first
        //////////////////////////////////////////////////////////////////////////

        CC_BREAK_IF(! CCLayer::init());

        //////////////////////////////////////////////////////////////////////////
        // add your codes below...
        //////////////////////////////////////////////////////////////////////////

//#if 1
		// Use batch node. Faster
		//CCSpriteBatchNode *parent = CCSpriteBatchNode::create("MainUILayer.png", 100);
		//m_pSpriteTexture = parent->getTexture();
//#else
		// doesn't use batch node. Slower
//#endif

		m_pSpriteTexture = CCTextureCache::sharedTextureCache()->addImage("HelloWorld.png");

		CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("color.plist");
		CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("shap.plist");


		uiLayer = UILayer::create();
		uiLayer->scheduleUpdate();
		uiLayer->addWidget(CCUIHELPER->createWidgetFromJsonFile("UI/GameUI.json"));
		addChild(uiLayer, 1);



		CCNode *parent = CCNode::create();
		addChild(parent, 0, uitag);

		forbidColor = _forbidColor;
		forbidShape	= _forbidShape;
		forbidNum	= _forbidNum;

		ResetForbid( uiLayer, forbidColor, forbidShape, forbidNum);

		hp = _hp;
		ResetHeartPanel( uiLayer, hp);

		ResetPoint(uiLayer, 0);


		//----------------------------------------------------------------
		UIButton* pauseBtn = (UIButton*)uiLayer->getWidgetByTag(62);
		pauseBtn->addReleaseEvent(this, menu_selector(MainUILayer::PauseGame) );
		pauseBtn->setPressedActionEnabled(true);
		pauseBtn->setEnabled(true);

		UIButton* resumeBtn = (UIButton*)uiLayer->getWidgetByTag(61);
		resumeBtn->addReleaseEvent(this, menu_selector(MainUILayer::ResumeGame) );
		resumeBtn->setPressedActionEnabled(true);
		resumeBtn->setEnabled(false);

		UIButton* backBtn = (UIButton*)uiLayer->getWidgetByTag(60);
		backBtn->addReleaseEvent(this, menu_selector(MainUILayer::OKAndBack) );
		backBtn->setPressedActionEnabled(true);
		backBtn->setEnabled(false);



		//----------------------------------------------------------------



		bRet = true;
    } while (0);

    return bRet;
}

void MainUILayer::menuCloseCallback(CCObject* pSender)
{
    // "close" menu item clicked
    CCDirector::sharedDirector()->end();
}


void MainUILayer::ChangeHp(int _hp)
{
	hp = _hp;

	ResetHeartPanel( uiLayer, hp);

	if(hp <=0 ){
		ShowGameOverUI();
	}
}


void MainUILayer::ShowGameOverUI()
{
	UILayer* ul =UILayer::create();
	ul->scheduleUpdate();
	ul->addWidget(CCUIHELPER->createWidgetFromJsonFile("UI/GameOverPopBorder.json"));
    addChild(ul, 1);

	UIButton* backBtn = (UIButton*)ul->getWidgetByTag(43);
	backBtn->addReleaseEvent(this, menu_selector(MainUILayer::OKAndBack) );

	UIButton* restartBtn = (UIButton*)ul->getWidgetByTag(42);
	//backBtn->addReleaseEvent(this, menu_selector(MainUILayer::OKAndBack) );


}



void MainUILayer::OKAndBack(cocos2d::CCObject* pSender)
{
	StageSelectScene* selectScn = new StageSelectScene;
	CCDirector::sharedDirector()->replaceScene(CCTransitionFade::create(1,selectScn ));
}


void MainUILayer::PauseGame(cocos2d::CCObject* pSender)
{
	UIButton* pauseBtn = (UIButton*)uiLayer->getWidgetByTag(62);
	pauseBtn->setEnabled(false);

	UIButton* resumeBtn = (UIButton*)uiLayer->getWidgetByTag(61);
	resumeBtn->setEnabled(true);

	UIButton* backBtn = (UIButton*)uiLayer->getWidgetByTag(60);
	backBtn->setEnabled(true);

	StageScene* gameScn = (StageScene*)CCDirector::sharedDirector()->getRunningScene();
	MainGameLayer*	gameLayer = gameScn->GetGameLayer();
	gameLayer->Pause();

}

void MainUILayer::ResumeGame(cocos2d::CCObject* pSender)
{
	UIButton* pauseBtn = (UIButton*)uiLayer->getWidgetByTag(62);
	pauseBtn->setEnabled(true);

	UIButton* resumeBtn = (UIButton*)uiLayer->getWidgetByTag(61);
	resumeBtn->setEnabled(false);

	UIButton* backBtn = (UIButton*)uiLayer->getWidgetByTag(60);
	backBtn->setEnabled(false);

	StageScene* gameScn = (StageScene*)CCDirector::sharedDirector()->getRunningScene();
	MainGameLayer*	gameLayer = gameScn->GetGameLayer();
	gameLayer->Resume();

}



void MainUILayer::ChangePoint(int point)
{
	ResetPoint(uiLayer, point);
}





